package Offer;

/*
* 从尾到头打印链表
*
*   栈，递归， 建立数组后从n-1开始反向插入
*
* */
public class Offer06reversePrint {
    public int[] reversePrint(ListNode head) {
        if (head == null) return new int[]{};
        int length = 0;
        ListNode cur = head;
        while (cur != null) {
            cur = cur.next;
            length++;
        }
        int[] result = new int[length];
        //反向插入
        for (int i = length - 1; i >= 0; i--) {
            result[i] = head.val;
            head = head.next;
        }
        return result;
    }
}
